//
//  FavouriteViewController.m
//  WuliHanhan
//
//  Created by chen tang on 16/2/26.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "FavouriteViewController.h"
#import "Header.h"
#import "UserInfoModel.h"

#import "MJRefresh.h"

#import "WuLiRequestFlowStar.h"
#import "WuLiRequestStarWorkAPI.h"


#import "StarZuopinScrollView.h"
#import "StarWorkCollectionViewCell.h"

#import "SearchStarViewController.h"
#import "ShowDetalImageViewController.h"

@interface FavouriteViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate, StarZuopinScrollViewDelegate>
{
    NSMutableArray *dataMutableArray;
    float lastContentOffset;
    NSMutableArray *starListArray;
    StarZuopinScrollView *guanzhuScrollView;
    UICollectionView *mainCollecTionView;
    UIView *guanzhuTitleView;
    
    NSString    *starIDString;
    NSString    *pageString;

}

//@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *mutableArray;





@end

@implementation FavouriteViewController


- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor redColor]];
    [self.navigationController setNavigationBarHidden:YES];

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WithView, 64)];
    [titleView setBackgroundColor:uicolor(254, 206, 0)];
    [self.view addSubview:titleView];
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, WithView, 25)];
    [titleLable setText:@"我的关注"];
    [self.view addSubview:titleLable];
    [titleLable setTextAlignment:NSTextAlignmentCenter];
    
    [self.view setBackgroundColor:uicolor(237, 237, 237)];
    starListArray = [NSMutableArray new];
    dataMutableArray = [NSMutableArray new];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    guanzhuTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleView.frame), WithView, 150.0)];
//    [guanzhuTitleView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:guanzhuTitleView];
    
    guanzhuScrollView = [[StarZuopinScrollView alloc] initWithFrame:CGRectMake(0, 0, WithView, 150.0)];
    [guanzhuTitleView setBackgroundColor:[UIColor whiteColor]];
    [guanzhuTitleView addSubview:guanzhuScrollView];
    
    UIView *lineVIew = [[UIView alloc] initWithFrame:CGRectMake(0, 149.5, WithView, 0.5)];
    [guanzhuTitleView addSubview:lineVIew];
    [lineVIew setBackgroundColor:uicolor(204, 204, 204)];
    
    UIImage *image = [UIImage imageNamed:@"NoData.png"];
    UIImageView *nodataImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [nodataImageView setCenter:CGPointMake(WithView/2.0, 310.0)];
    [nodataImageView setImage:image];
    [self.view addSubview:nodataImageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nodataImageView.frame)+14.0, WithView, 20.0)];
    [lable setTextAlignment:NSTextAlignmentCenter];
    [lable setTextColor:uicolor(97, 97, 97)];
    [self.view addSubview:lable];
    [lable setText:@"暂无该明星作品"];

    
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    mainCollecTionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 222, WithView, heightView-49-64) collectionViewLayout:flowLayout];
    [self.view addSubview:mainCollecTionView];
    
    [mainCollecTionView setBackgroundColor:[UIColor redColor]];
    [mainCollecTionView registerClass:[StarWorkCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(WithView-40, 20, 40, 40)];
    [button setTitle:@"更多" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(pushSearchStarVC) forControlEvents:UIControlEventTouchUpInside];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    pageString = @"1";
    [WuLiRequestFlowStar requestFlowStarWithUserID:[UserInfoModel userIDString]];
    
    [guanzhuScrollView setMStarZuopinScrollViewDelegate:self];
    [mainCollecTionView setBackgroundColor:uicolor(237, 237, 237)];
    [mainCollecTionView setDataSource:self];
    [mainCollecTionView setDelegate:self];
    
    [self addJianting];
    
    [guanzhuScrollView.carousel scrollToItemAtIndex:0 animated:YES];
    
    
    mainCollecTionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewMyWorkioData)];
    mainCollecTionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreMyWorkioData)];
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController setHidesBottomBarWhenPushed:NO];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//坚挺
- (void)addJianting
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserFloeStarBack:) name:getFloeStar object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(starWorkGetBack:) name:mingxingzuopin object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkFieldGetBack) name:@"netfield" object:nil];
}



#pragma mark     --------    dataSource


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(4, 5, 4, 5);
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataMutableArray.count;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(biliwith(146.0), biliwith(242.0));
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";
    StarWorkCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setTag:indexPath.row];
    [cell loadModel:dataMutableArray[indexPath.row]];
    return cell;
}



#pragma mark----获得关注明星
- (void)getUserFloeStarBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
    NSLog(@"%@", dic);
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
            [starListArray removeAllObjects];
            [starListArray addObjectsFromArray:[dic valueForKey:@"data"]];

            [guanzhuScrollView reloadWithArray:starListArray];
        [guanzhuTitleView setHidden:NO];
        [mainCollecTionView setHidden:NO];
        
        if (starListArray.count == 1)
        {
            starIDString = [[starListArray objectAtIndex:0] valueForKey:@"id"];            
            [mainCollecTionView.mj_header beginRefreshing];
//            [mainCollecTionView setHidden:NO];
        }
        else if (starListArray.count == 0)
        {
            [mainCollecTionView setHidden:YES];
            [guanzhuTitleView setHidden:YES];
        }
        else
        {
//            [mainCollecTionView setHidden:NO];
        }

    }
    else
    {
        
    }
}



//网络请求失败
- (void)netWorkFieldGetBack
{
    if (![pageString isEqualToString:@"1"])
    {
        pageString = [NSString stringWithFormat:@"%d", [pageString intValue] - 1];
    }
    [mainCollecTionView.mj_footer endRefreshing];
    [mainCollecTionView.mj_header endRefreshing];
}


//刷新明星作品
- (void)loadNewMyWorkioData
{
    pageString = @"1";
    [WuLiRequestStarWorkAPI requestStarWithWork:starIDString pageString:pageString];
}

//获取更多作品
- (void)loadMoreMyWorkioData
{
    pageString = [NSString stringWithFormat:@"%d", [pageString intValue] + 1];
    [WuLiRequestStarWorkAPI requestStarWithWork:starIDString pageString:pageString];
}


//点击获取明星
- (void)requestStarWork:(NSString *)IDString
{
    starIDString = IDString;
    [mainCollecTionView.mj_header beginRefreshing];
}


#pragma mark-----获取明星作品
- (void)starWorkGetBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
    [mainCollecTionView.mj_header endRefreshing];
    [mainCollecTionView.mj_footer endRefreshing];
    
//    NSLog(@"dic ===== %@",dic);
    
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        if([pageString isEqualToString:@"1"])
        {
            if ([[dic valueForKey:@"data"] count] == 0)
            {
                [mainCollecTionView setHidden:YES];
            }
            else
            {
                [mainCollecTionView setHidden:NO];
                [dataMutableArray removeAllObjects];
                [dataMutableArray addObjectsFromArray:[dic valueForKey:@"data"]];
                [mainCollecTionView reloadData];
            }
            
        }
        else
        {
            if ([[dic valueForKey:@"data"] count] == 0)
            {
                pageString = [NSString stringWithFormat:@"%d", [pageString intValue] - 1];
            }
            [mainCollecTionView setHidden:NO];
//            [dataMutableArray removeAllObjects];
            [dataMutableArray addObjectsFromArray:[dic valueForKey:@"data"]];
            [mainCollecTionView reloadData];

        }
    }
    else
    {
        
    }
}



//进入明星列表
- (void)pushSearchStarVC
{
    SearchStarViewController *searchStarView = [[SearchStarViewController alloc] init];
    [self.navigationController pushViewController:searchStarView animated:YES];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    lastContentOffset = scrollView.contentOffset.y;
}


//滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 64) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame;
            frame = guanzhuTitleView.frame;
            CGFloat delta = frame.origin.y + 150;
            frame.origin.y = MAX(-150, frame.origin.y - delta);
            guanzhuTitleView.frame = frame;
            guanzhuTitleView.alpha = 0;
            [mainCollecTionView setFrame:CGRectMake(0, 64, WithView, heightView-49.0-64)];
        }];
    }
    
    if (lastContentOffset - 100 > scrollView.contentOffset.y) {
        [UIView animateWithDuration:0.3 animations:^{
            [mainCollecTionView setFrame:CGRectMake(0, 222, WithView, heightView-49.0-64)];
            CGRect frame;
            frame = guanzhuTitleView.frame;
            //            CGFloat delta = frame.origin.y - 150;
            frame.origin.y = MIN(0, 214);
            guanzhuTitleView.frame = CGRectMake(0, 64, WithView, 150);
            guanzhuTitleView.alpha = 1.0;
        }];

    }
    if (scrollView.contentOffset.y <= 0) {
        [UIView animateWithDuration:0.3 animations:^{
            [mainCollecTionView setFrame:CGRectMake(0, 222, WithView, heightView-49.0-64)];
            CGRect frame;
            frame = guanzhuTitleView.frame;
            //            CGFloat delta = frame.origin.y - 150;
            frame.origin.y = MIN(0, 214);
            guanzhuTitleView.frame = CGRectMake(0, 64, WithView, 150);
            guanzhuTitleView.alpha = 1.0;
        }];
    }
    
}



//进入作品详情
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"data ===== %@",dataMutableArray);
//    NSLog(@"%@", dataMutableArray[indexPath.row]);
    ShowDetalImageViewController *detalView = [[ShowDetalImageViewController alloc] init];
    detalView.IDString = [NSString stringWithFormat:@"%@", [[dataMutableArray objectAtIndex:indexPath.row] valueForKey:@"id"]];
    detalView.textStr = [[dataMutableArray objectAtIndex:indexPath.row]valueForKey:@"title"];
    [self.navigationController pushViewController:detalView animated:YES];

}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
